package com.coocaa.ops.admin.v1.homepage.provider.impl;

import com.coocaa.ops.admin.base.AbstractBizImpl;
import com.coocaa.ops.admin.base.exception.AdminBizException;
import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.base.service.AbstractSaveServiceImpl;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.audit.AuditUtil;
import com.coocaa.ops.admin.v1.audit.provider.BaseAuditTaskProviderImpl;
import com.coocaa.ops.admin.v1.dmp.util.DmpRegistryRlsUtil;
import com.coocaa.ops.admin.v1.homepage.biz.impl.HomepageInfoBizImpl;
import com.coocaa.ops.admin.v1.homepage.biz.impl.HomepageModuleRlsBizImpl;
import com.coocaa.ops.admin.v1.homepage.provider.IHomepageInfoRlsUpdateProvider;
import com.coocaa.ops.admin.v1.homepage.provider.IHomepageInfoUpdateProvider;
import com.coocaa.ops.common.core.base.enums.ResourceStatusEnums;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.biz.audit.entity.AuditTaskInfoEntity;
import com.coocaa.ops.common.core.biz.homepage.entity.HomepageInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @author bjh
 * @description 首页信息原始表-更新provider接口实现
 * @date : 2020-06-15 16:31:15
 */
@Service
public class HomepageInfoUpdateProviderImpl extends AbstractSaveServiceImpl<HomepageInfoEntity> implements IHomepageInfoUpdateProvider {
    @Autowired
    private HomepageInfoBizImpl thisBizImpl;
    @Autowired
    private IHomepageInfoRlsUpdateProvider thisRlsUpdateProvider;
    @Autowired
    private HomepageModuleRlsBizImpl homepageModuleRlsBiz;
    private static CommonLanguageProperties commonLanguageProperties;

    @Override
    public AbstractBizImpl getAbstractBizImpl() {
        return thisBizImpl;
    }

    @Override
    public void handleSaveRlsInfo(HomepageInfoEntity inputEntity, Integer userId) {
        thisRlsUpdateProvider.saveRls(inputEntity, userId);
    }

    /**
     * 保存
     *
     * @param inputEntity
     * @param userId
     * @return
     */
    @Override
    public HomepageInfoEntity save(HomepageInfoEntity inputEntity, Integer userId) {
        this.saveBaseInfo(inputEntity.getId(), inputEntity, userId);
        return inputEntity;
    }


    /**
     * 批量删除
     *
     * @param ids
     * @param userId
     * @return
     */
    @Override
    public String deleteBatch(Integer[] ids, Integer userId) {
        for (Integer id : ids) {
            HomepageInfoEntity entity = (HomepageInfoEntity) thisBizImpl.queryById(id);
            Assert.notNull(entity, commonLanguageProperties.getHomepage() + commonLanguageProperties.getNotNull());
            if (ResourceStatusValidator.isOnline(entity.getStatus())) {
                return "[ID：" + id + "][" + commonLanguageProperties.getName() + entity.getHomepageName() + "]" + commonLanguageProperties.getHomepage() + commonLanguageProperties.getAuditSuccess() + commonLanguageProperties.getNotDelete();

            }
            if (thisBizImpl.isHomeHasUsed(id)) {
                return "[ID：" + entity.getId() + "][" + commonLanguageProperties.getName() + entity.getHomepageName() + "]" + commonLanguageProperties.getHomepage() + commonLanguageProperties.getInUse() + commonLanguageProperties.getNotDelete();
            }
            //判断板块是否已提交流程
            AuditTaskInfoEntity taskInfo = AuditUtil.getTaskByIdAndVersionAndType(entity.getId(), entity.getCurrentVersion(), ResourceTypeEnum.HOMEPAGE.getValue());
            if (taskInfo != null && taskInfo.getTaskFlag() == 1) {
                return "[ID：" + id + "][" + commonLanguageProperties.getName() + entity.getHomepageName() + "]" + commonLanguageProperties.getHomepage() + commonLanguageProperties.getSubmitReview() + commonLanguageProperties.getNotDelete();
            }
            if (InputParamsValidator.isDuplicateExist(entity.getDuplicateVersion())) {
                taskInfo = AuditUtil.getTaskByIdAndVersionAndType(entity.getId(), entity.getDuplicateVersion(), ResourceTypeEnum.HOMEPAGE.getValue());
                if (taskInfo != null && taskInfo.getTaskFlag() == 1) {
                    return "[ID：" + id + "][" + commonLanguageProperties.getName() + entity.getHomepageName() + "]" + commonLanguageProperties.getHomepage() + "[V" + entity.getDuplicateVersion() + "]copy" + commonLanguageProperties.getSubmitReview() + commonLanguageProperties.getNotDelete();
                }
            }
            thisBizImpl.updateFlag(id, userId, ResourceStatusEnums.OFF_LINE.getStatus());
            //删除关联信息
            homepageModuleRlsBiz.logicDelRls(id, entity.getCurrentVersion(), userId);
        }
        return R.SUCCESS;

    }

    @Override
    public void deleteHistory(Integer id, Integer version, Integer userId) {
        HomepageInfoEntity homepageInfoEntity = (HomepageInfoEntity) thisBizImpl.queryById(id);
        Assert.notNull(homepageInfoEntity, commonLanguageProperties.getHomepage() + commonLanguageProperties.getNotNull());
        if (!BaseAuditTaskProviderImpl.V_1.equals(version) && homepageInfoEntity.getCurrentVersion().equals(version)) {
            //如果不是V1版本，而且和当前版本一样
            throw new AdminBizException(commonLanguageProperties.getCopy() + commonLanguageProperties.getSubmitReview() + commonLanguageProperties.getNotDelete());
        }
        //处理v1待审核或者草稿版本的删除
        this.handleDelHistoryForV1(homepageInfoEntity);
        if (BaseAuditTaskProviderImpl.V_1.equals(version)) {
            //V1的处理
            thisBizImpl.updateFlag(id, userId, ResourceStatusEnums.OFF_LINE.getStatus());
        }
        //更新副本删除状态
        thisBizImpl.updateHistoryFlag(id, version, userId, ResourceStatusEnums.OFF_LINE.getStatus());
        //更新原始表信息
        HomepageInfoEntity update = new HomepageInfoEntity();
        update.setId(id);
        //置空副本标识
        update.setDuplicateVersion(null);
        update.setModifier(userId);
        thisBizImpl.updateById(update);
        //删除关联信息
        homepageModuleRlsBiz.logicDelRls(id, version, userId);
        DmpRegistryRlsUtil.deleteDmpRls(id, ResourceTypeEnum.HOMEPAGE.getValue(), version);
    }


    @Override
    public void handleCurrentEditAndCopyEdit(Integer autoIncrementId, HomepageInfoEntity inputEntity, Integer userId) {
        super.handleCurrentEditAndCopyEdit(autoIncrementId, inputEntity, userId);
        //删除关联信息
        thisRlsUpdateProvider.deleteAllRls(autoIncrementId, inputEntity.getCurrentVersion());
    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/
    /**
     * 处理v1待审核或者草稿版本的删除
     *
     * @param entity
     */
    private void handleDelHistoryForV1(HomepageInfoEntity entity) {
        if (BaseAuditTaskProviderImpl.V_1.equals(entity.getCurrentVersion()) && ResourceStatusValidator.isToAuditingOrDraft(entity.getStatus())) {
            //已经被使用不能删除
            if (thisBizImpl.isHomeHasUsed(entity.getId())) {
                throw new AdminBizException("[ID：" + entity.getId() + "][" + commonLanguageProperties.getName() + entity.getHomepageName() + "]" + commonLanguageProperties.getHomepage() + commonLanguageProperties.getInUse() + commonLanguageProperties.getNotDelete());
            }
        }
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        HomepageInfoUpdateProviderImpl.commonLanguageProperties = commonLanguageProperties;
    }

}
